﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Database;
using Company.Database.Entities;
using Company.Shell.Dialogs;
using Company.Shell.Interfaces;
using HandyControl.Controls;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    public class UserViewModel : ObservableObject
    {
        private string message = string.Empty;
        public string Message
        {
            get { return message; }
            set { SetProperty(ref message, value); }
        }
        private List<UserEntity>? users;
        public List<UserEntity>? Users
        {
            get { return users; }
            set { SetProperty(ref users, value); }
        }
        
        public string Role { get; set; } = "操作员";
       
        public UserEntity LoginUserEntity { get; set; } = new UserEntity();
        public UserEntity NewUserEntity { get; set; } = new UserEntity();
        public ICommand? LoginCommand { get;  }
        public ICommand? AddUserCommand { get; }
        public ICommand? SaveUserCommand { get; }
        public ICommand? DeleteUserCommand { get; }
        public ISession Session { get; }
        private IUserRepository UserRepository { get;  }

        public UserViewModel(ISession session, IUserRepository userRepository)
        {
            Session = session;
            UserRepository = userRepository;
            LoginCommand = new RelayCommand(OnLoginCommand);
            AddUserCommand = new RelayCommand(OnAddUserCommand);
            SaveUserCommand = new RelayCommand(OnSaveUserCommand);
            DeleteUserCommand = new RelayCommand<UserEntity>(OnDeleteUserCommand);
        }

        private void OnDeleteUserCommand(UserEntity user)
        {
            if (user == null) return;

            // 防止删除当前登录用户
            if (Session.CurrentUser != null && Session.CurrentUser.Id == user.Id)
            {
                Dialog.Show(new MessageDialog("不能删除当前登录用户"));
                return;
            }

            // 确认删除对话框
            var result = System.Windows.MessageBox.Show(
                $"确认删除用户 '{user.UserName}' 吗？",
                "确认删除",
                System.Windows.MessageBoxButton.YesNo,
                System.Windows.MessageBoxImage.Question);

            if (result == System.Windows.MessageBoxResult.Yes)
            {
                var success = UserRepository.Delete(user);
                if (success)
                {
                    Dialog.Show(new MessageDialog("删除成功"));
                    Users = UserRepository.GetAll(); // 刷新用户列表
                }
                else
                {
                    Dialog.Show(new MessageDialog("删除失败"));
                }
            }
        }

        private void OnSaveUserCommand()
        {
            if (Users == null) return;
            foreach (var user in Users)
            {
                UserRepository.Update(user);
            }
            Dialog.Show(new MessageDialog("保存成功"));
            
        }

        private void OnAddUserCommand()
        {
            if (string.IsNullOrEmpty(NewUserEntity.UserName))
            {
                Dialog.Show(new MessageDialog("请输出用户名"));
                return;
            }
            if (string.IsNullOrEmpty(NewUserEntity.Password))
            {
                Dialog.Show(new MessageDialog("请输出密码"));
                return;
            }
            var entity = new UserEntity
            {
                UserName = NewUserEntity.UserName,
                Password = NewUserEntity.Password,
                Role = Role == "操作员" ? 0 : 1
            };
            var success = UserRepository.Insert(entity);
            if (success)
            {
                Dialog.Show(new MessageDialog("添加成功"));
                Users = UserRepository.GetAll();
            }
            else
            {
                Dialog.Show(new MessageDialog("添加失败"));
            }
        }

        private void OnLoginCommand()
        {
            Message = "";
            var success = UserRepository.Login(LoginUserEntity.UserName, LoginUserEntity.Password,out UserEntity? user);
            if (success)
            {
                Session.Add(user);
                Message = "登录成功";
                LoginUserEntity.UserName = "";
                LoginUserEntity.Password = "";
            }
            else
            {
                Message = "登录失败";
            }
        }
    }
    
}
